<?php

class Login extends CI_Controller {

    function index() {
        if ($this->session->userdata('isLoggedIn')) {
            //redirect('/main/show_main');
            $this->show_login(false);
        } else {
            $this->show_login(false);
        }
    }

    function threads() {
        $this->load->view('threads');
    }

    function forums() {
        $this->load->view('forums');
    }

    function topics() {
        $this->load->view('topics');
    }

    function profile() {
        $data['title'] = "Welcome user";
        $this->load->model('users_mgr');
        $data['user'] = $this->users_mgr->select_all();

        $this->load->view('profile', $data);
    }

    function register() {
		if ($this->input->post('submit_register')) {
			$this->load->library("form_validation");
			$this->form_validation->set_rules('fname', 'Firstname', 'trim|required');
			$this->form_validation->set_rules('lname', 'Lastname', 'trim|required');
			$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_alpha_numeric|min_length[6]|xss_clean|strtolower|callback_username_not_exists');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[6]|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_alpha_numeric|min_length[6]|matches[con_password]|xss_clean');
			$this->form_validation->set_rules('con_password', 'Confirm_password', 'trim|required|alpha_alpha_numeric|min_length[6]');

			if ($this->form_validation->run() == FALSE) {
				$data['title'] = "Welcome to register";
				$this->load->view('register', $data);
			} else {
				if ($this->user_m->register()) {
					redirect('login');
				} else {
					$data['user_level'] = $this->user_m->user_level();
					$this->load->view('register', $data);
				}
			}
		} else {
			$data['user_level'] = $this->user_m->user_level();
			$this->load->view('register', $data);
		}
    }

    function login_user() {
        // Create an instance of the user model
        $this->load->model('user_m');
        // Grab the email and password from the form POST
        $email = $this->input->post('email');
        $pass = $this->input->post('password');
        if ($email) {
            //Ensure values exist for email and pass, and validate the user's credentials
            if ($email && $pass && $this->user_m->validate_user($email, $pass)) {
                $this->session->set_userdata('user_login', $email);
                // If the user is valid, redirect'' to the main view
                if ($this->session->userdata('user_login') == "admin@example.com") {
                    redirect('admin/dashboard');
                } else {
					redirect('login/profile');
                }
            } else {
                // Otherwise show the login screen with an error message.
                $this->show_login(true);
            }
        } else {
            $this->show_login(true);
        }
    }

    function show_login($show_error = false) {
        $data['error'] = $show_error;

        $this->load->helper('form');
        $this->load->view('login', $data);
    }

    function logout_user() {
        $this->session->sess_destroy();
        $this->index();
    }

    function update() {
		$data['title'] = 'Update user';
		if ($this->input->post('btn_update')) {
			echo "TODO: Action Update";
		} else {
			$data['edit'] = $this->user_m->select_edit_user();
			$this->load->view('update', $data);
		}
    }

    function remove_user() {
        $this->load->model('user_m');
        $email = $this->session->userdata('user_login');
        if ($email) {
            $success = $this->user_m->delete_users($email);
            if ($success) {
                redirect('login/');
            } else {
                echo "Error";
            }
        }
    }

}